package com.easepal802s.project.ui.activity;

import android.os.Bundle;
import android.os.Handler;

import androidx.appcompat.app.AppCompatActivity;
import android.util.Log;

import com.easepal7506a.project.manager.DataManager;
import com.easepal7506a.project.network.CommondManager;
import com.easepal7506a.project.observer.UpdataObserver;
import com.easepal802s.project.R;
import com.easepal802s.project.utils.ActivityUtils;
import com.ogawa.base.callback.GetUpdataCallback;
import com.ogawa.base.network.BaseResponse;
import com.ogawa.base.network.bean.BeanUpdata;
import com.ogawa.base.utils.CommonUtil;
import com.ogawa.base.utils.ProjectSPUtils;
import com.ogawa.base.utils.statusbar.SystemStatusBarUtil;


/**
 * 启动页
 */
public class SplashActivity extends AppCompatActivity {
    private String mSaveVersion="";
    private int mSaveCount=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_splash);
        SystemStatusBarUtil.setStatusBar(this,  R.color.transparent, false);
        mSaveVersion = ProjectSPUtils.INSTANCE.getSaveVersion();
        mSaveCount = ProjectSPUtils.INSTANCE.getSaveCount();
        initView();
        checkUpdata();

    }

    private void checkUpdata(){
        if(CommonUtil.isNetworkConnected(this)&&CommonUtil.isNetworkOnline()){
            //检查网络更新
            CommondManager.getInst().getUpdata(this, new GetUpdataCallback() {
                @Override
                public void onGetOnlineSuccess(BaseResponse<BeanUpdata> program) {
                    DataManager.getInst().setHasCheckUpdataOk(true);
                    DataManager.getInst().setmUpdataBean(program.getData());
                    BeanUpdata bean = program.getData();
                    if(bean.getIsupgrade()==1){
                        if(bean.getIsforce()==1){
                            ProjectSPUtils.INSTANCE.setSaveVersion(bean.getAppversion());
                            ProjectSPUtils.INSTANCE.setSaveCount(0);
                            UpdataObserver.getInst().noticeonUpdata(program.getData());
                        }
                        else {
                            if(!mSaveVersion.equals(bean.getAppversion())){
                                ProjectSPUtils.INSTANCE.setSaveVersion(bean.getAppversion());
                                ProjectSPUtils.INSTANCE.setSaveCount(1);
                                UpdataObserver.getInst().noticeonUpdata(program.getData());
                            }else if(mSaveCount<=3){
                                ProjectSPUtils.INSTANCE.setSaveCount(mSaveCount+1);
                                UpdataObserver.getInst().noticeonUpdata(program.getData());
                            }
                        }


                    }

                }

                @Override
                public void onGetOnlineFailure() {
                    DataManager.getInst().setHasCheckUpdataOk(false);
                    Log.e("checkUpdata","checkUpdata_onGetOnlineFailure");
                }
            });
        }
        else {
            DataManager.getInst().setmUpdataBean(null);
            DataManager.getInst().setHasCheckUpdataOk(false);

        }
    }




    private void initView() {


        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                ActivityUtils.startChooseActivity(SplashActivity.this);
                finish();

            }
        }, 3000);
    }
}
